package jc;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

public class SoftReferenceDemo {
	public static void main(String args[]){
		// memory_Enough();
		// memory_NotEnough();
		// weakReference();
		// weakHashMap();
		System.out.println("1n1111ame".matches("^[a-zA-Z_]+\\w*$"));

	}

	public static void weakHashMap() {
		Map<Integer, String> map = new WeakHashMap<>();
		Integer i = new Integer("1");
		map.put(i, "1");
		System.out.println(map);
		i = null;
		System.gc();
		System.out.println(map);
	}
	public static void memory_Enough() {
		Object o = new Object();
		SoftReference<Object> objectSoftReference = new SoftReference<>(o);
		System.gc();
		o = null;
		System.gc();
		System.out.println(o);
		System.out.println(objectSoftReference.get());
	}

	public static void memory_NotEnough() {
		Object o = new Object();
		SoftReference<Object> objectSoftReference = new SoftReference<>(o);
		o = null;
		try {
			// byte[] b = new byte[30 * 1024 * 1024];
			System.out.println(Runtime.getRuntime().maxMemory());
			System.out.println(Runtime.getRuntime().totalMemory());
			System.out.println(Runtime.getRuntime().freeMemory());
			TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println(o);
			System.out.println(objectSoftReference.get());
		}
	}

	public static void weakReference() {
		Object o = new Object();
		WeakReference weakReference = new WeakReference(o);
		o = null;
		System.gc();
		System.out.println(o);
		System.out.println(weakReference.get());
	}
}
